
package Designation;

import java.sql.ResultSet;
import javax.swing.JOptionPane;
import connectDB.ConnectDB;
import java.sql.PreparedStatement;
import java.sql.Statement;


public class Des_Insert extends javax.swing.JFrame {
    ConnectDB connect = new ConnectDB();
    public Des_Insert() {
        
        connect.getConnect();
        initComponents();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtDesIdInsert = new javax.swing.JTextField();
        txtDesNameInsert = new javax.swing.JTextField();
        btnDesInsert = new javax.swing.JButton();
        btnDesReset = new javax.swing.JButton();
        btnDesCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtDesNameInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDesNameInsertActionPerformed(evt);
            }
        });

        btnDesInsert.setText("Insert");
        btnDesInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesInsertActionPerformed(evt);
            }
        });

        btnDesReset.setText("Reset");
        btnDesReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesResetActionPerformed(evt);
            }
        });

        btnDesCancel.setText("Cancel");
        btnDesCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesCancelActionPerformed(evt);
            }
        });

        jLabel1.setText("DESIGNID");

        jLabel2.setText("DESIGNATION");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(btnDesInsert)
                .addGap(53, 53, 53)
                .addComponent(btnDesReset, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(btnDesCancel)
                .addGap(39, 39, 39))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDesIdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDesNameInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(103, 103, 103))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDesIdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDesNameInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDesInsert)
                    .addComponent(btnDesReset)
                    .addComponent(btnDesCancel))
                .addContainerGap(153, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtDesNameInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDesNameInsertActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDesNameInsertActionPerformed

    private void btnDesInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesInsertActionPerformed
        // TODO add your handling code here:
        checkinsertDes();
        
        
    }//GEN-LAST:event_btnDesInsertActionPerformed

    private void btnDesCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnDesCancelActionPerformed

    private void btnDesResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesResetActionPerformed
        // TODO add your handling code here:
        resetDes();
    }//GEN-LAST:event_btnDesResetActionPerformed

    
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Des_Insert().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDesCancel;
    private javax.swing.JButton btnDesInsert;
    private javax.swing.JButton btnDesReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtDesIdInsert;
    private javax.swing.JTextField txtDesNameInsert;
    // End of variables declaration//GEN-END:variables

    private void insertDes(){
        String id = txtDesIdInsert.getText();
        String name = txtDesNameInsert.getText();
        try {
            String query = "INSERT INTO Designation VALUES (?,?)";
            
            PreparedStatement pst = connect.cn.prepareStatement(query);
            pst.setString(1, id);
            pst.setString(2, name);
            
           
            
            pst.executeUpdate();
            
            JOptionPane.showMessageDialog(this, "Insert sucessful");
           
            
            
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Insert fail");
        }
    }
    private void resetDes(){
        txtDesIdInsert.setText("");
        txtDesNameInsert.setText("");
    }
    private void checkinsertDes(){
        if (txtDesIdInsert.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "ID cannot blank");
                return;
            }
        try {
            String query10 = "Select DESIGNID from Designation";
            Statement st = connect.cn.createStatement();
            ResultSet rs = st.executeQuery(query10);
            while (rs.next()) {
                if (txtDesIdInsert.getText().equals(rs.getString("DESIGNID"))) {
                    JOptionPane.showMessageDialog(this, "ID be duplicated");
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(txtDesIdInsert.getText().length() > 10){
                JOptionPane.showMessageDialog(this, "ID can not be longer than 10 characters");
                return;
        }
        
        
        if (txtDesNameInsert.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Name cannot blank");
                return;
            }
        if(txtDesNameInsert.getText().length() > 15){
                JOptionPane.showMessageDialog(this, "Name can not be longer than 15 characters");
                return;
        }
        insertDes();
        
    }

}
